/**
 *
 */
package cn.hg.jfsa.manage.controller.login;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;

import cn.hg.base.common.BaseConstants;
import cn.hg.base.jfinal.controller.BaseController;
import cn.hg.base.kit.PasswordKit;
import cn.hg.jfsa.common.JfsaConstants;
import cn.hg.jfsa.common.JfsaLogManager;
import cn.hg.jfsa.core.JfsaThreadLocal;
import cn.hg.jfsa.manage.controller.login.validator.ManageLoginValidator;
import cn.hg.jfsa.manage.service.JfsaResourceService;
import cn.hg.jfsa.model.JfsaAdmin;
import cn.hg.jfsa.model.JfsaLogLogin;
import cn.hg.jfsa.model.JfsaResource;

/**
 * @author Golly
 */
public class ManageLoginController extends BaseController {

	@Inject
	private JfsaResourceService resourceService;

	/*
	 * (non-Javadoc)
	 * @see cn.hg.base.controller.BaseController#index()
	 */
	@Override
	@Before (ManageLoginValidator.class)
	public void index () {

		if (this.getRequest().getMethod().equals("GET")) { // GET请求则打开登录页面
			this.setAttr("account", this.getCookie("cookie_account"));
			this.render("login.html");
		} else if (this.getRequest().getMethod().equals("POST")) { // 如果是POST请求则执行登录操作
			String account = this.getPara("account");
			String password = this.getPara("password");

			if (!this.validateCaptcha("captcha")) {
				this.setAttr("errMsg", "验证码错误！");
				this.render("login.html");
				return;
			}

			// 获取用户
			JfsaAdmin admin = JfsaAdmin.DAO.findFirst("SELECT * FROM jfsa_admin WHERE status<>'del' AND account=?",
				account);

			if (null == admin) {
				this.setAttr("errMsg", "此用户不存在！");
				this.render("login.html");
				return;
			}

			if ("off".equals(admin.getStatus())) {
				this.setAttr("errMsg", "此用户已禁用，请联系管理员！");
				this.render("login.html");
				return;
			}

			// 密码判定
			if (!PasswordKit.validatePassword(admin.getSalt(), password, admin.getPassword())) {
				this.setAttr("errMsg", "账号或密码不正确！");
				this.render("login.html");
				return;
			}

			// 登录成功则更新用户登录信息
			admin.setLoginIp(this.getIPAddress()).setLoginTime(new Date()).setTotalLogins(admin.getTotalLogins() + 1)
				.update();
			// 记录日志
			JfsaLogLogin logItem = new JfsaLogLogin()
				.setAdminId(admin.getId())
				.setLoginIp(this.getIPAddress())
				.setLoginTime(new Date());
			JfsaLogManager.me().add(logItem);
			Map<String, JfsaResource> resourceMap = this.resourceService.getActionByAdmin(admin);
			List<JfsaResource> menuList = this.resourceService.getMenuTreeByAdmin(admin);
			// 将数据保存在Session中
			JfsaThreadLocal.setAdmin(admin);
			JfsaThreadLocal.setAdminAuthMenu(menuList);
			JfsaThreadLocal.setAdminAuthAction(resourceMap);

			// 记住账号
			if (this.getParaToBoolean("rememberMe")) {
				this.setCookie("cookie_account", admin.getAccount(), 3600 * 24 * 30);
			}

			// 跳转到首页
			this.redirect("/manage");
		}

	}

	/**
	 * 登出
	 */
	public void logout () {
		this.setSessionAttr(BaseConstants.SESSION_ADMIN, null);
		this.setSessionAttr(JfsaConstants.AUTH_ACTION_MAP, null);
		this.redirect("/manage/login");
	}

}
